package com.ib.algokit.history;
import static org.activequant.util.tools.IdentityUtils.equalsTo;
import static org.activequant.util.tools.IdentityUtils.safeCompare;
import static org.activequant.util.tools.IdentityUtils.safeHashCode;

import java.io.Serializable;

import org.activequant.core.domainmodel.InstrumentSpecification;
import org.activequant.core.types.TimeStamp;

import com.ib.algokit.InstrumentAnalyzer;
import com.ib.algokit.forex.ForexPairStates;
/**
* A TradeEvent subclass. Holds the following inherited variables:
* <ul>
* <li>id(Long)</li>
* <li>eventTimeStamp(TimeStamp)</li>
* <li>message(String)</li>
* <li>instrumentSpecification(InstrumentSpecification)</li>
* </ul>
*  @author dmisev
*/
public final class StopLossClosedEvent extends TradeEvent implements Serializable, Comparable<StopLossClosedEvent> {

	private static final long serialVersionUID = -1L;
	/**
	 * private ForexPairStates forexPairStates=null;
	 */
	private ForexPairStates forexPairStates=null;
	
    private double loss=0;
        
    private double successRatio=0;
    
    private TimeStamp openingTimeStamp=null;
	/**
	 * Creates empty order accept event.<br/>
	 * An empty StopLossClosedEvent(TradeEvent subclass) constructor
	 */
	public StopLossClosedEvent() { }
	/**
	 * constructs an StopLossClosedEvent(TradeEvent subclass) using the given stamp(TimeStamp) and message(String) to set its
	 * inherited eventTimeStamp(TimeStamp) and message(String).<br/>
	 * Sets its associated instrumentSpecification(InstrumentSpecification) with the given instrumentSpecification(InstrumentSpecification)
	 * @param stamp time stamp.
	 * @param message message.
	 * @param instrumentSpecification the spec for which this event happened
	 */
	public StopLossClosedEvent(TimeStamp stamp, TimeStamp openingTimeStamp, String message, InstrumentSpecification instrumentSpecification, ForexPairStates forexPairStates, double loss, double successRatio) {
		super(stamp, message, instrumentSpecification);
		this.forexPairStates=forexPairStates;
        this.loss=loss;
        this.successRatio=successRatio;
        this.openingTimeStamp=openingTimeStamp;
	}
	
	public double getLoss(){
        return loss;
    }
    
	public double getSuccessRatio(){
        return successRatio;
    }
	
	public ForexPairStates getForexPairStates(){
        return forexPairStates;
    }
        
    public long getDiffMin(){
       long diffMin=(getEventTimeStamp().getNanoseconds()-openingTimeStamp.getNanoseconds())/(InstrumentAnalyzer.ONE_MINUTE);    	   
       return diffMin;
    }

	public TimeStamp getOpeningTimeStamp(){
        return openingTimeStamp;
    }
	/**
	 * returns a String in the form "StopLossClosedEvent: eventTimeStamp=${eventTimeStamp}, message=${message}, spec=${instrumentSpecification}"
	 */
	public String toString() {
		return "StopLossClosedEvent: opened=" + getOpeningTimeStamp().getDate()+ ", closed=" + getEventTimeStamp().getDate()+ ", min=" + getDiffMin()+ ", successRatio=" + getSuccessRatio() + /*", message=" + getMessage()+*/ ", spec=" + getInstrumentSpecification();
	}
   /**
	 * {@inheritDoc}
	 */
	public int hashCode() {
		// ATTENTION: keep in sync with compareTo()
		return safeHashCode(this.getEventTimeStamp())
		+ safeHashCode(this.getOpeningTimeStamp())
		+ safeHashCode(this.successRatio)
		+ safeHashCode(this.loss)
		+ safeHashCode(this.getInstrumentSpecification());
	}
	/**
	 * {@inheritDoc}
	 */
	public int compareTo(StopLossClosedEvent other) {
		// ATTENTION: keep in sync with hashCode();
		int rc;
		rc = safeCompare(this.getEventTimeStamp(), other.getEventTimeStamp());
		if(rc != 0) return rc;
		rc = safeCompare(this.getOpeningTimeStamp(), other.getOpeningTimeStamp());
		if(rc != 0) return rc;
		rc = safeCompare(this.successRatio, other.successRatio);
		if(rc != 0) return rc;
		rc = safeCompare(this.loss, other.loss);
		if(rc != 0) return rc;
		rc = safeCompare(this.getInstrumentSpecification(), other.getInstrumentSpecification());
		if(rc != 0) return rc;
		return rc;
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean equals(Object other) {
		// NOTE: delegates to compareTo()
		return equalsTo(this, other);
	}
}
